package view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import model.*;
import controller.*;

/*****************************************
 * gamePanel
 * 
 * Responsible for displaying any graphics on 
 * the screen
 * 
 * @author Stephen Gregory, Jun Wang
 *
 */

public class gamePanel extends JPanel implements KeyListener {

		private static final long serialVersionUID = -7820991932017770969L;
		private userInputController inputController;
		private gameModelI model;
		BufferedImage bkgd;
		BufferedImage ice;
		BufferedImage ice_r;
		BufferedImage ice_o;
		BufferedImage ice_g;
		BufferedImage ice_p;
		BufferedImage character;
		Font gameFont;
		JButton quitButton = new JButton("Quit");
		JButton leaderboardButton = new JButton("Quit to Leaderboard");
		private BufferedImage life_img;

		public gamePanel(gameModelI m, userInputController u) {
			this.model = m;
			this.inputController = u;
			setBackground(Color.white);
			setLayout(null);
			quitButton.setBounds(950, 580, 60, 30);
			leaderboardButton.setBounds(800, 580, 150, 30);
			add(leaderboardButton);
	        add(quitButton);
			setPreferredSize(new Dimension(1000, 600));	
			addKeyListener(this);
			bkgd = null;	
			ice = null;
			gameFont = new Font(Font.SANS_SERIF, Font.BOLD, 14);
			loadImages();
		}
		
		private void loadImages() {
			try {
				bkgd = ImageIO.read(new File("bkgd2.jpg"));
				ice = ImageIO.read(new File("log3.png"));
				ice_r = ImageIO.read(new File("iceflow_red.png"));
				ice_o = ImageIO.read(new File("iceflow_orange.png"));
				ice_g = ImageIO.read(new File("iceflow_green.png"));
				ice_p = ImageIO.read(new File("iceflow_pink.png"));
				character = ImageIO.read(new File("penguin.png"));
				life_img = ImageIO.read(new File("penguin_sm.png"));
			} catch (IOException e) {}			
		}
	
	    public void paintComponent(Graphics g) {
	    	super.paintComponent(g);
			g.drawImage(bkgd, 0, 0, null);
	    	g.setColor(Color.black);
	    	ArrayList<log> list = model.getLogs();
	    	frog gameFrog = model.getFrog();
	    	g.setFont(gameFont);
	    	g.setColor(Color.red);
	    	g.drawString("Stage " + model.getStage(), 50, 20);
	    	
	    	for (int i = 0; i < model.getLives(); i++)
	    		g.drawImage(life_img, 840-30*i, 5, null);
	    	
	    	g.drawString("Score: " + model.getCurScore(), 900, 20);
	    	
	    	for (int i = 0; i < list.size(); i++) {	    		
	    		if (i != 25) {	
	    			//check for the type of log
	    			if (list.get(i).getType() == 0) 
		    			g.drawImage(ice, list.get(i).getPosX(), list.get(i).getPosY(), null);
	    			else if (list.get(i).getType() == 1)			
		    			g.drawImage(ice_o, list.get(i).getPosX(), list.get(i).getPosY(), null);
	    			else if (list.get(i).getType() == 2)
		    			g.drawImage(ice_g, list.get(i).getPosX(), list.get(i).getPosY(), null);
	    			else if (list.get(i).getType() == 3)
  		    			g.drawImage(ice_p, list.get(i).getPosX(), list.get(i).getPosY(), null);
	    			else
		    			g.drawImage(ice_r, list.get(i).getPosX(), list.get(i).getPosY(), null);
	    		
	    		}	    		
	    		g.setColor(Color.black);
	            g.drawString(list.get(i).getWord(), list.get(i).getPosX()+20, list.get(i).getPosY()+20);	        
	    	}
	    	
			g.drawImage(character, gameFrog.getPosX(), gameFrog.getPosY(), null);
			
	    	//highlights letters as they are typed	    	
	    	if((model.getActiveLog()!=null)){
	    		log activelog = model.getActiveLog();
	    		String word = activelog.getWord();

	    		char[] wordArray;
	    		wordArray = word.toCharArray();
	    		
	    		FontMetrics fm = g.getFontMetrics();
	    		int deltaX = fm.charsWidth(wordArray, 0, model.getNumLettersTyped());  
	    		//draw untyped letters
	    		g.setColor(Color.black);
	    		g.drawChars(wordArray, model.getNumLettersTyped(),activelog.getWord().length()-model.getNumLettersTyped(), activelog.getPosX()+20+ deltaX, activelog.getPosY()+20);
	    		//draw typed letters
	    		g.setColor(Color.red);
	    		g.drawChars(wordArray, 0, model.getNumLettersTyped(), activelog.getPosX()+20, activelog.getPosY()+20);
	    	}
	    }
	    
	    public void addQuitListener(ActionListener a) {
	        quitButton.addActionListener(a);
	    }
	    
	    public void addLeaderboardListener(ActionListener a) {
	        leaderboardButton.addActionListener(a);
	    }
		
		@Override
		public boolean isFocusTraversable() {
		  return true;
		}

		@Override
		public void keyPressed(KeyEvent keyinput) {
			//Note: we should filter out special characters in second iteration
			inputController.processInput(keyinput.getKeyChar());
		}
		
		@Override
		public void keyReleased(KeyEvent arg0) {}
		
		@Override
		public void keyTyped(KeyEvent arg0) {}
    }